import 'dart:math';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:im_flutter_sdk_example/jsonserialize/group/data.dart';
import 'package:im_flutter_sdk_example/jsonserialize/group/group_manager.dart';
import 'package:im_flutter_sdk_example/utils/global.dart';
import 'package:image_picker/image_picker.dart';

final String defaultLogo =
    'https://static.kuaimayoupin.com/9599402988136831.png';

class GroupFormDataModel with ChangeNotifier {
  String summary = '';
  String name = '';
  String note = '';
  String webLink = '';
  String value = '';
  String logo = 'https://static.kuaimayoupin.com/9599402988136831.png';
  bool hasGroup = false;
  Group myGroup = new Group(logo: defaultLogo);
  GroupMgrModel groupManager = GroupMgrModel();
  final picker = ImagePicker();

//获取我的小组信息
  getMyGroup() async {
    var res = await G.req.group.groupMy();
    if (res == null) return;
    GroupModel groupM = GroupModel.fromJson(res.data);
    if (groupM.status == 1 && groupM.data.length > 0) {
      myGroup = groupM.data[0];
      print('myGroup ${myGroup.owner}');
      hasGroup = true;
    }
    // var r = await G.req.group.members(groupId: "${myGroup.id}");
    // GroupMgrModel mgrM = GroupMgrModel.fromJson(r.data);
    // if (mgrM.status == 1) {
    //   mgrM.data.forEach((manager) {
    //     if (manager.role == 1) {
    //       manager.checked = true;
    //     }
    //   });
    //   myGroupManager = mgrM.data;
    // }
    // print('mgrM res: ${mgrM.message}');
    notifyListeners();
  }

// 获取 管理员列表
  getManagers() async {
    var res = await G.req.group.managers();
    groupManager = GroupMgrModel.fromJson(res.data);
    notifyListeners();
  }

  int randomNumber() {
    var rng = new Random();
    return rng.nextInt(100000000);
  }

  onImagePress() async {
    PickedFile pickedFile = await picker.getImage(source: ImageSource.gallery);
    String path = pickedFile.path;
    DateTime now = DateTime.now();
    FormData formdata = FormData.fromMap(
      {
        "file": await MultipartFile.fromFile(path,
            filename:
                "${now.year}_{now.month}_${now.day}_${randomNumber()}.png"),
        'scene': 'other'
      },
    );
    var res = await G.req.group.upload(file: formdata);
    res.data['data'].forEach((item) => {myGroup.logo = item['url']});
    notifyListeners();
  }

  void submit() async {
    Map<String, String> formData = {
      "summary": "${myGroup.summary}",
      "name": "${myGroup.name}",
      "note": "${myGroup.note}",
      "web_link": "${myGroup.webLink}",
      "logo": "${myGroup.logo}",
    };
    await G.req.group.create(formdata: formData);
    // 刷新 小组信息
    await getMyGroup();
    G.pop();
  }

  void setValue(String key) {
    switch (key) {
      case 'name':
        value = myGroup.name;
        break;
      case 'summary':
        value = myGroup.summary;
        break;
      case 'note':
        value = myGroup.note;
        break;
      case 'webLink':
        value = myGroup.webLink;
        break;
      default:
    }
    notifyListeners();
  }

  void setName(String value, String key) {
    print(value);
    value = value;
    switch (key) {
      case 'name':
        myGroup.name = value;
        break;
      case 'summary':
        myGroup.summary = value;
        break;
      case 'note':
        myGroup.note = value;
        break;
      case 'webLink':
        myGroup.webLink = value;
        break;
      default:
    }
    notifyListeners();
  }
}
